from datetime import datetime
from typing import List, Optional,Union

from fastapi.exceptions import HTTPException

from app.core.crud import CRUDBase
from app.models.intelligence_model import IntelligenceModel
from app.schemas.login import CredentialsSchema,JWTOut
from app.schemas.models import ModelCreate, ModelUpdate
from app.utils.password import get_password_hash, verify_password

from .role import role_controller


class ModelController(CRUDBase[IntelligenceModel, ModelCreate, ModelUpdate]):
    def __init__(self):
        super().__init__(model=IntelligenceModel)

    async def get_by_email(self, email: str) -> Optional[IntelligenceModel]:
        return await self.model.filter(email=email).first()

    async def get_by_modelname(self, modelname: str) -> Optional[IntelligenceModel]:
        return await self.model.filter(modelname=modelname).first()

    async def create_model(self, obj_in: ModelCreate) -> IntelligenceModel:
        # obj_in.password = get_password_hash(password=obj_in.password)
        obj = await self.create(obj_in)
        return obj

    async def update_last_login(self, id: int) -> None:
        model = await self.model.get(id=id)
        model.last_login = datetime.now()
        await model.save()


model_controller = ModelController()
